package com.hyyt.shopjsp.store.info.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.product.pojo.ShopProductInfo;
import com.hyyt.shopjsp.product.service.IShopProductInfoService;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.jcaptcha.QRCode;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicArea;
import com.hyyt.shopjsp.basic.pojo.BasicCustomer;
import com.hyyt.shopjsp.basic.service.IBasicAreaService;
import com.hyyt.shopjsp.basic.service.IBasicCustomerService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.store.info.pojo.ShopShopInfo;
import com.hyyt.shopjsp.store.info.pojo.ShopShopCategory;
import com.hyyt.shopjsp.store.info.service.IShopShopInfoService;
import com.hyyt.shopjsp.store.info.service.IShopShopCategoryService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/04/11 0024下午 17:54.
 */

@Controller
@RequestMapping("/back/enterShopInfo")
public class ShopShopInfoController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopShopInfoController.class);
	private String basePath = "/back/shop/shopinfo/";
	@Resource
	private IShopShopCategoryService shopShopCategoryService;//店铺分类service
	@Resource
	private IShopShopInfoService shopShopInfoService;//店铺service
	@Resource
	private IBasicAreaService areaService;//地区service
	@Resource
	private IBasicCustomerService basicCustomerService;//会员service
	@Resource
	private IShopProductInfoService shopProductInfoService;//商品service

	/**
	 * 进入入驻店铺信息管理
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoEnterShopInfoPage")
	public String gotoEnterShopInfoPage(Map<String, Object> param){
		Map<String, Object> params=new HashMap<>();
		List<ShopShopCategory> shopCategoryList=shopShopCategoryService.queryShopCategoryList(params);
		List<BasicCustomer> customerList=basicCustomerService.queryAllBasicCustomer();
		param.put("shopCategoryList", shopCategoryList);
		param.put("customerList", customerList);
		return basePath+"shopInfoList";
	}

	/**
	 * 查询所有店铺分类
	 * @param page
	 * @param response
	 * @param shopName			店铺名
	 * @param customerName		店铺账号
	 * @param isClose			是否关闭
	 * @param isPass			店铺分类
	 * @param shopCategoryId	审核状态
	 */
	@RequestMapping(value="listShopInfo",method=RequestMethod.POST)
	@ResponseBody
	public void listShopInfo(Page page,Integer shopInfoType,String shopName,String customerName,String isClose,String isPass,String shopCategoryId, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopInfoType)){
			param.put("shopInfoType", shopInfoType);
		}
		if(Utils.objectIsNotEmpty(customerName)){
			param.put("customerName", customerName.trim());
		}
		if(Utils.objectIsNotEmpty(shopName)){
			param.put("shopName", shopName.trim());
		}
		if(Utils.objectIsNotEmpty(isPass)){
			param.put("isPass", isPass);
		}
		if(Utils.objectIsNotEmpty(isClose)){
			param.put("isClose", isClose);
		}
		if(Utils.objectIsNotEmpty(shopCategoryId)){
			param.put("shopCategoryId", shopCategoryId);
		}
		param.put("sortColumns", "shopInfoId desc");
		int totalRecordCount =shopShopInfoService.countShopShopInfo(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopShopInfo> shopInfoListMap =shopShopInfoService.queryShopShopInfo(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", shopInfoListMap);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}
	/**
	* 根据店铺分类id获得对象
	*
	* @param shopInfoId
	*/
	@RequestMapping(value = "getShopInfoObject", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg getShopInfoObject(Integer shopInfoId) {
		Map<String, Object> param=new HashMap<>();
		if (Utils.objectIsNotEmpty(shopInfoId)) {
			ShopShopInfo shopInfo  = shopShopInfoService.loadShopShopInfo(shopInfoId);//查询店铺信息
			if(Utils.objectIsNotEmpty(shopInfo) && Utils.objectIsNotEmpty(shopInfo.getShopCategoryId())){
				ShopShopCategory shopCategory = shopShopCategoryService.loadShopShopCategory(shopInfo.getShopCategoryId());//查询店铺分类
				if(Utils.objectIsNotEmpty(shopCategory.getShopCategoryName())){
					param.put("categoryName",shopCategory.getShopCategoryName());//获得分类名
				}
			}
			param.put("shopInfo", shopInfo);
			if(Utils.objectIsNotEmpty(shopInfo) && Utils.objectIsNotEmpty(shopInfo.getRegionLocation())){
				BasicArea basicArea = areaService.loadBasicArea(Integer.parseInt(shopInfo.getRegionLocation()));//州省地区
				if(Utils.objectIsNotEmpty(basicArea)&&Utils.objectIsNotEmpty(basicArea.getName())){
					param.put("regionLocation", basicArea.getName());
				}
			}
			if(Utils.objectIsNotEmpty(shopInfo.getCity())){
				BasicArea basicArea = areaService.loadBasicArea(Integer.parseInt(shopInfo.getCity()));//查询城市
				if(Utils.objectIsNotEmpty(basicArea.getName())){
					param.put("city", basicArea.getName());
				}
			}
			if(Utils.objectIsNotEmpty(shopInfo.getAreaCounty())){
				BasicArea basicArea = areaService.loadBasicArea(Integer.parseInt(shopInfo.getAreaCounty()));//查询国家
				if(Utils.objectIsNotEmpty(basicArea)){
					param.put("areaCounty", basicArea.getName());
				}
			}
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(param));
		}
		return ResultMsg.create(false);
	}

	/**
	 * 保存或修改店铺信息
	 * @return
	 */
	@RequestMapping(value="saveOrUpdateShopInfo",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateShopInfo(ShopShopInfo shopInfo){
		boolean flag=false;
		if(Utils.objectIsNotEmpty(shopInfo)){
			flag=shopShopInfoService.updateShopShopInfo(shopInfo);
		}
		return ResultMsg.create(flag);
	}

	/**
	 * 保存审核店铺信息
	 * @return
	 */
	@RequestMapping(value="saveCheckShopInfo",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveCheckShopInfo(String shopInfoId,String phoneShowStatus,String commission,String isPass){
		boolean flag=false;
		String sessionUsersName = getSessionUsersName();
		if(Utils.objectIsNotEmpty(shopInfoId) && Utils.objectIsNotEmpty(phoneShowStatus) && Utils.objectIsNotEmpty(commission) &&Utils.objectIsNotEmpty(isPass) && Utils.objectIsNotEmpty(sessionUsersName)){
			Map<String,Object> params = new HashMap<>();
			params.put("shopInfoId",shopInfoId);
			params.put("phoneShowStatus",phoneShowStatus);
			params.put("commission",commission);
			params.put("isPass",isPass);
			if("3".equals(isPass)){
				params.put("shopInfoCheckStatus","2");
				params.put("verifier",sessionUsersName);
				//从redis中获取手机微信项目路径
				String basePath = RedisDataReadUtil.getSysConfig("phoneBasePath");
				//组拼二维码的路径
				String addUrl = basePath + StringStaticKey.WX_PHONE_SHOP_INFO_ID_URL+ shopInfoId;
				//生成二维码
				String codeUrl= QRCode.createQRPng(addUrl,null,RedisDataReadUtil.getSysConfig("fileUploadRoot"),"shop","image_shopInfo");
				params.put("qrCode",codeUrl);
			}else if("2".equals(isPass)){
				params.put("verifier",sessionUsersName);
				params.put("shopInfoCheckStatus","3");
			}
			flag=shopShopInfoService.saveCheckShopInfo(params);
		}
		return ResultMsg.create(flag);
	}

	/**
	 * 批量升级为自营店铺
	 * @return 返回值
	 */
	@RequestMapping(value="upGradeSelfSupportShop",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg upGradeSelfSupportShop(String shopInfoIds){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopInfoIds)){
			param.put("shopInfoIds",shopInfoIds.split(","));
			shopShopInfoService.updateShopShopInfoByShopInfoIds(param);
			return ResultMsg.create(true);
		}else{
			return ResultMsg.create(false);
		}
	}

	/**
	 * 批量开启或关闭店铺
	 * @return
	 */
	@RequestMapping(value="saveOrUpdateIsClose",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateIsClose(String shopInfoIds,String isClose){
		boolean flag=false;
		String [] shopInfoId = shopInfoIds.split(",");
		for (int i=0; i< shopInfoId.length;i++) {
			if(Utils.objectIsNotEmpty(isClose)&&Utils.objectIsNotEmpty(shopInfoId[i])){
				ShopShopInfo shopInfo=shopShopInfoService.loadShopShopInfo(Integer.parseInt(shopInfoId[i]));
				shopInfo.setIsClose(Integer.parseInt(isClose));
				shopShopInfoService.updateShopShopInfo(shopInfo);
				flag=true;
			}
		}
		return ResultMsg.create(flag);
	}

	public String getFileUrlConfig() {
		String uploadFileVisitRoot = RedisDataReadUtil.getSysConfig(StringStaticKey.FILE_UPLOAD_VISIT_ROOT);
		return uploadFileVisitRoot;
	}

	/**
	 * 更换店铺类型
	 * @param shopInfoId	店铺id
	 * @param shopInfoType	店铺类型:1为入驻店铺,2为自营店铺
	 * @return
	 */
	@RequestMapping(value="updateShopInfoType",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg updateShopInfoType(String shopInfoId,String shopInfoType){
		boolean flag=false;
		if(Utils.objectIsNotEmpty(shopInfoId)&&Utils.objectIsNotEmpty(shopInfoType)){
			ShopShopInfo shopInfo=shopShopInfoService.loadShopShopInfo(Integer.parseInt(shopInfoId));
			shopInfo.setShopInfoType(Integer.parseInt(shopInfoType));
			shopShopInfoService.updateShopShopInfo(shopInfo);
			flag=true;
		}
		return ResultMsg.create(flag);
	}

	/**
	 * 进入入驻店铺内商品信息
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoEnterShopProductListPage")
	public String gotoEnterShopProductListPage(Map<String, Object> param,String shopInfoId){
		param.put("shopInfoId", shopInfoId);
		return basePath+"enterShopProductList";
	}

	/**
	 * 根据店铺ID查询该店铺的商品
	 * @param page
	 * @param productName	商品名称
	 * @param sku			商品
	 * @param isPass		审核状态
	 * @param isPutSale		是否上架
	 */
	@RequestMapping("queryProductList")
	@ResponseBody
	public void queryProductList(Page page,String shopInfoId,String productName,String sku,Integer isPass,Integer isPutSale, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopInfoId)){
			param.put("shopInfoId",shopInfoId);
		}
		if(Utils.objectIsNotEmpty(productName)){
			param.put("productName",productName.trim());
		}
		if(Utils.objectIsNotEmpty(sku)){
			param.put("sku",sku.trim());
		}
		if(Utils.objectIsNotEmpty(isPass)){
			param.put("isPass",isPass);
		}
		if(Utils.objectIsNotEmpty(isPutSale)){
			param.put("isPutSale",isPutSale);
		}
		param.put("sortColumns", "productId desc");
		int totalRecordCount =shopProductInfoService.countShopProductInfo(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopProductInfo> productInfoList =shopProductInfoService.queryProductList(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", productInfoList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

}
